package com.cinco.blog.dao;

import com.cinco.blog.po.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface BlogDao extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {

    @Query("select new Blog (id,title,content,firstPicture,flag,views,createTime,updateTime,description,tagIds) from Blog order by createTime desc ")
    List<Blog> findAllwx();

    @Query("select new Blog (id,title,content,firstPicture,flag,views,createTime,updateTime,description,tagIds) from Blog where id = ?1 ")
    Blog getByIdWx(Long id);

    @Query("select b from Blog b where b.recommend = true")
    List<Blog> findTop(Pageable pageable);

    @Query("select b from Blog b where b.title like ?1 or b.content like ?1 or b.description like ?1")
    Page<Blog> findByQuery(String query,Pageable pageable);

    @Modifying
    @Query("update Blog b set b.views = b.views +1 where b.id = ?1")
    int updateViews(Long id);

    @Query("select function('date_format',b.createTime,'%Y') as year from Blog b group by year order by year desc ")
    List<String> findGroupYear();

    @Query("select b from Blog b where function('date_format',b.createTime,'%Y') = ?1 order by b.createTime desc ")
    List<Blog> findByYear(String year);
}
